package com.panshi.utils;

import com.panshi.vo.UserVO;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class GetSessionUser {
    /**
     * 获取session里的User  --登录的user
     * @return
     */
    public static UserVO getSessionUser(){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        return (UserVO)request.getSession().getAttribute("loginUser");
    }

    /**
     * 获取session用户的id
     * @return
     */
    public static int getSessionUserId(){
        if(getSessionUser() == null) {
            return 0;
        }else {
            return  getSessionUser().getU_id();
        }
    }
}
